package Postgre;


import org.testng.annotations.*;

import java.sql.*;

import static org.testng.Assert.*;
import static org.testng.Assert.assertEquals;



@Test()
public class testGetTablesPostgre {

    private Connection con;

    @BeforeClass
    public void connect() throws ClassNotFoundException, SQLException {
        Class.forName("org.postgresql.Driver");
        con = DriverManager.getConnection("jdbc:postgresql://localhost:5432/test", "andrey", "andrey");

        Statement st = con.createStatement();
        st.executeUpdate("create table test1 (id integer, value varchar(80))");
        st.executeUpdate("create table test2 (id integer, value integer)");
    }
    
    @AfterClass
    public void closeConnection() throws SQLException {
        Statement st = con.createStatement();
        st.execute("drop table test1");
        st.execute("drop table test2");

        con.close();
    }

    @Test
    public void testGetTables() throws SQLException {
        String tbName;
        Statement st = con.createStatement();

        ResultSet rs = st.executeQuery("SELECT  t.table_name\n" +
                "   FROM information_schema.TABLES t\n" +
                "  WHERE t.table_schema::text = 'public'::text AND \n" +
                "        t.table_catalog::name = current_database() AND \n" +
                "        t.table_type::text = 'BASE TABLE'::text AND \n" +
                "        NOT \"substring\"(t.table_name::text, 1, 1) = '_'::text\n" +
                "  ORDER BY t.table_name;");
        
        rs.next();
        tbName = rs.getString("table_name");
        System.err.println("Table name: " + tbName);
        assertEquals(tbName, "test1");

        rs.next();
        tbName = rs.getString("table_name");
        System.err.println("Table name: " + tbName);
        assertEquals(tbName, "test2");
    }
}
